import 'package:flutter/material.dart';
import 'package:icooker/configs/mock_datas.dart';
import 'package:icooker/model/reommend_data.dart';
import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 基于: flutter_ijkplayer
/// https://pub.flutter-io.cn/packages/flutter_ijkplayer
class FlutterIJKPlayerView extends StatefulWidget {
  VideoInfoData videoInfo;

  FlutterIJKPlayerView(this.videoInfo, {Key? key}) : super(key: key);

  @override
  _FlutterIJKPlayerViewState createState() => _FlutterIJKPlayerViewState();
}

class _FlutterIJKPlayerViewState extends State<FlutterIJKPlayerView> {
  IjkMediaController controller = IjkMediaController();
  late Future<void> _initPlayerFuture;

  @override
  void initState() {
    super.initState();
    var videoUrl = URL_HTTP_VIDEOS[3];
    _initPlayerFuture =
        controller.setNetworkDataSource(widget.videoInfo.video!.vendorVideo!,
            // videoUrl,
            autoPlay: true);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var viddeoInfo = widget.videoInfo.video;
    int? width, height;
    if (viddeoInfo != null) {
      width = viddeoInfo.width;
      height = viddeoInfo.height;
    }
    // double videoWidth = ScreenUtil().screenWidth;
    // double videoHeight = ScreenUtil().screenHeight;
    // if (width != null) {
    //   videoWidth = width as double;
    // }
    // if (height != null) {
    //   videoHeight = height as double;
    // }

    return Scaffold(
      body: FutureBuilder(
        future: _initPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Container(
              child: buildIjkPlayer(),
            );
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }

  Center buildCenter() {
    return Center(
      child: AspectRatio(
        aspectRatio: controller.videoInfo.ratio,
        child: buildIjkPlayer(),
      ),
    );
  }

  IjkPlayer buildIjkPlayer() {
    return IjkPlayer(
      mediaController: controller,
    );
  }
}
